#!/bin/bash

checkcmp(){
 local NAME=$1
 local SRC=$NAME.hs
 local BIN=$OUT/$NAME.out
 local TEST=test

 [ $SRC -nt $BIN -o ! -f $BIN -o -z "$ONCE" ] &&
 {
  touch $BIN
  ONCE=1
  printf "$NAME "
  date
   OK=""
   if
    if
     grep "`echo -e '\x09'`" $SRC
    then
	 echo error: tab detected
	 break 1
	else
	 ghc -o $BIN $SRC # -E
	fi
   then
    for a in $TEST/*
    do
     echo " $a/in"
     if
      # runhaskell $OPT $SRC
      diff -u $a/out <( $BIN < $a/in )
     then
      : #echo test ok
     else
      OK=`echo $OK $a`
     fi
    done
    if [ -z "$OK" ]
    then
     echo passed
    else
     echo failed "$OK"
    fi
   fi
#   rm -f $NAME.hc $NAME.hi a.out
  }
}

main(){
 OUT="./"
 TOWATCH="hills"
 ONCE=""
# echo ready
 while true
 do
  for a in $TOWATCH
  do
   checkcmp $a
  done
  sleep 0.1
 done
}

main "$@"
# xrhjfbcxmhoi
